<!--登陆页面-->
<template>

  <div id="login">
    <el-row>
      <el-col :span="20" :offset="12">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="帐号" prop="account">
        <el-input v-model="ruleForm.account" placeholder="请输入帐号或邮箱"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" placeholder="请输入密码"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="success" @click="submitForm('ruleForm')">登陆</el-button>
        <router-link to="/register">
        <el-button type="danger">注册</el-button>
        </router-link>
      </el-form-item>
    </el-form>
      </el-col>
    </el-row>
  </div>

</template>
<script>
  import {User} from "../req/User";
  import {ContentType} from "../../constant/ContentType";
  import {ResultStatus} from "../../constant/ResultStatus";
  import {StringUtil} from "../../../utils/StringUtil";

  export default {
    data() {
      //帐户检验
      var account = (rule,value,callback) => {
        if(value === ''){
          callback (new Error('请输入帐号'));
        }

        callback();
      }
      //密码校验
      var password = (rule,value,callback)=>{
        if(value === ''){
          callback(new Error('请输入密码'));
        }
        callback();
      }
      return {
        ruleForm: {

          account:'',
          password:'',
        },
        rules: {
         account:[
           {validator:account,trigger:'blur'}
         ],
          password:[
            {validator:password,trigger:'blur'}
          ]

        }
      };
    },
    methods: {

      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            var self = this;
            let user = new User();
            user.id = this.ruleForm.account;
            user.password = this.ruleForm.password;
            self.$axios({
              method: 'post',
              url:'/user/login',
              data:user,
              dataType: 'json',
              headers: ContentType.APPLICATION_JSON,
            }).then((response) => {

                if(response.data.code == ResultStatus.SUCCESS){
                  //将用户信息存入store中
                  this.$store.dispatch("setUserInfo", response.data.data);
                  //存入cookie
                  document.cookie=ResultStatus.SSO_TOKEN_KEY+"="+response.data.data.key;
                  // 登陆成功 跳转首页
                  this.$router.push("/");
                  location.reload();

                }
                else{
                  this.$notify(StringUtil.warning(response.data.msg));
                }
            }).catch((error) => {
              console.log(self.qs)
              console.log(error)
              return false;
            });
          }
          else {
            console.log('error submit!!');
            return false;
          }
        });
      },

    }
  }
</script>

<style>
  #login{
   width: 50%  }
</style>
